package cn.darkhorse.mesadmin.controller.ticket;


import cn.darkhorse.ticket.basis.pojo.TicketProductType;
import cn.darkhorse.ticket.basis.service.TicketProductTypeService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/mesticket/basisProductType")
public class TicketProductTypeController extends BaseController{
	
	@Resource
	private TicketProductTypeService productTypeService;

	@Resource
	RepeatCheck<TicketProductType> repeatCheck;
	
	/**
	 * 列表
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public TableDataInfo list(String name){
		startPage();
		LambdaQueryWrapper<TicketProductType> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(name)){
			queryWrapper.like(TicketProductType::getName,name);
		}
		queryWrapper.orderByDesc(TicketProductType::getGmtCreate);
		List<TicketProductType> productTypeList = productTypeService.list(queryWrapper);
		return getDataTable(productTypeList);
	}
	
	/**
	 * 保存
	 * @param ticketProductType
	 * @return
	 */
	@RepeatSubmit(interval = 2000)
	@Transactional
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public AjaxResult add(@RequestBody @Validated TicketProductType ticketProductType){
		//编码、名称判重
	/*	String s = productTypeService.repeat(ticketProductType);
		if(StringUtils.isNotBlank(s)){
			ticketProductType.setCode(s);
		}*/
		String s = repeatCheck.repeat(productTypeService, ticketProductType, TicketConstants.BUSINESS_TYPE_CPFL);
		if(StringUtils.isNotBlank(s)){
			ticketProductType.setCode(s);
		}
		productTypeService.save(ticketProductType);
		return AjaxResult.success(s);
	}
	
	/**
	 * 修改
	 * @param ticketProductType
	 * @return
	 */
	@RepeatSubmit(interval = 2000)
	@RequestMapping(value = "/modify",method = RequestMethod.POST)
	public AjaxResult modify(@RequestBody  @Validated TicketProductType ticketProductType){
		boolean quote = productTypeService.isQuote(ticketProductType.getId());
		if(!quote){
			return AjaxResult.success("该分类已被产品引用，无法操作！");
		}
		//名称判重
		//productTypeService.repeat(ticketProductType);
		repeatCheck.repeat(productTypeService, ticketProductType, TicketConstants.BUSINESS_TYPE_CPFL);
		productTypeService.updateById(ticketProductType);
		return AjaxResult.success();
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RepeatSubmit(interval = 2000)
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public AjaxResult delete(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		boolean quote = productTypeService.isQuote(id);
		if(!quote){
			return AjaxResult.success("该分类已被产品引用，无法操作！");
		}
		boolean b = productTypeService.removeById(id);
		return AjaxResult.success(b);
	}
	
	/**
	 * 是否被引用
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/is-quote",method = RequestMethod.GET)
	public AjaxResult isQuote(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		boolean quote = productTypeService.isQuote(id);
		return AjaxResult.success(quote);
	}
	
	
}

